<template>
	<view>
		<view class="userinfo">
			<view class="user">
				<u-avatar mode="circle" size="150" :src="userInfo.avatar"></u-avatar>
				<view class="info">
					<text>{{userInfo.nickname}}</text>
					<view class="vip">
						<image src="../../static/images/VIP1.png" mode="" style="width: 40rpx;height: 40rpx;">
						</image>
						<text class="viptext">{{userVip.name}}</text>
					</view>
				</view>
			</view>
			<view class="tip">
				<text v-if="userVip.price==0">尚未开通会员，赶紧开通会员享受更多权益吧！</text>
				<text v-else>你的会员将于{{userVip.closeTime}}到期哦！</text>
			</view>
		</view>
		<view class="card">
			<swiper class="swiper" :current="1" next-margin="40rpx" previous-margin="40rpx" @change="changeSwiper">
				<swiper-item v-for="(item,index) in list" :key="index">
					<view class="vip-card">
						<view class="head">
							<text>{{item.name}}</text>
						</view>
						<view class="cont">
							<u-row gutter="16" v-if="item.id!=1">
								<u-col span="4" v-for="(it,ix) in item.rules" :key="ix">
									<view class="item" @click="selopen(item,it)">
										<view class="item-head"
											:style="{'background':item.id==selvip.id&&selvip.month==it.month?'#ff9900':'#999999'}">
											<text>{{it.month}}个月</text>
										</view>
										<view class="item-cont"
											:style="{'borderColor':item.id==selvip.id&&selvip.month==it.month?'#ff9900':'#e6e6e6'}">
											<text>￥{{it.price}}</text>
										</view>
									</view>
								</u-col>
							</u-row>
							<view class="order" v-if="item.id!=1">
								<view class="order-head">
									<text>支付方式</text>
								</view>
								<view class="pay" @click="selPay(1)">
									<view class="left">
										<u-image src="/static/images/pay_wx.png" width="30rpx" height="30rpx"></u-image>
										<text style="margin-left: 10rpx;">微信支付</text>
									</view>
									<u-icon name="checkmark-circle-fill" :color="pay_type==1?'#f44c01':'#999999'"
										size="40"></u-icon>
								</view>
								<!-- #ifdef APP-PLUS -->
								<view class="pay" @click="selPay(2)">
									<view class="left">
										<u-image src="/static/images/pay_ali.png" width="30rpx" height="30rpx">
										</u-image>
										<text style="margin-left: 10rpx;">支付宝支付</text>
									</view>
									<u-icon name="checkmark-circle-fill" :color="pay_type==2?'#f44c01':'#999999'"
										size="40"></u-icon>
								</view>
								<!-- #endif -->
							</view>
							<view v-if="item.id!=1 && !userVip.closeTime" style="margin-top: 30rpx;margin-bottom: 30rpx;">
								<u-button type="warning" style="width: 600rpx;" @click="openVip">立即开通
								</u-button>
							</view>
							<u-divider color="#ff9900">专享权益</u-divider>
							<u-grid :col="2" :border="false">
								<u-grid-item>
									<text class="grid-num">{{item.jobCount}}</text>
									<view class="grid-text">招聘免费发布数/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.roomCount}}</text>
									<view class="grid-text">房源免费发布数/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.carCount}}</text>
									<view class="grid-text">车辆免费发布数/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.goodsCount}}</text>
									<view class="grid-text">物品免费发布数/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.nongCount}}</text>
									<view class="grid-text">农林免费发布数/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.businessCount}}</text>
									<view class="grid-text">生意免费发布数/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.smsCount}}</text>
									<view class="grid-text">推广免费发布数/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.speakCount}}</text>
									<view class="grid-text">打听免费发布数/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.carToPeopleCount}}</text>
									<view class="grid-text">车找人免费发布数/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.peopleToCarCount}}</text>
									<view class="grid-text">人找车免费发布数/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.topCount/10}}折</text>
									<view class="grid-text">置顶折扣/月</view>
								</u-grid-item>
								<u-grid-item>
									<text class="grid-num">{{item.moreCount}}</text>
									<view class="grid-text">超过免费发布数价格/条</view>
								</u-grid-item>
							</u-grid>
						</view>
					</view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	import {
		mapActions,
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				userVip: {
					vip_id: 1,
					vip_time: 0,
					name: "普通会员"
				},
				list: [],
				selvip: {},
				pay_type: 1,
			};
		},
		computed: {
			...mapState({
				userInfo: state => state.user.userInfo,
				isLogin: state => state.user.isLogin,
			})
		},
		onLoad() {
			this.$u.get('/home/memberLevel/list', {

			}).then(res => {
				this.list = res;
				console.log(this.list[1])
				this.selvip = {
					id: this.list[1].id,
					...this.list[1].rules[0]
				}
				this.selvip.id=this.list[1].id;
				console.log(this.selvip)
			})
		},
		onShow() {
			if (this.isLogin) {
				this.$u.get('/home/level/getCateFreeNum', {type:1

				}).then(res => {
					this.userVip = res
				})
			}
		},
		methods: {
			selopen(item, it) {
				let obj = {
					id: item.id,
					...it,
				}
				obj.id= item.id;
				this.selvip = obj;
			},
			changeSwiper(e) {
				if (e.detail.current >= 1) {
					let obj = {
						id: this.list[e.detail.current].id,
						...this.list[e.detail.current].rules[0],
					}
					this.selvip = obj;
				}
			},
			openVip() {
				this.$u.post('/tc/openVip', {
					...this.selvip
				}).then(res => {
					var that = this;
					if(res==-1){
					 that.$u.toast('您已开通过会员了！')
					}
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							console.log(loginRes);
							that.$u.post('/tc/payOpenVip', {
								code: loginRes.code,
								id: res,
								pay_type: that.pay_type,
								//#ifdef MP-WEIXIN
								platform: 'mini_app_wx',
								//#endif
							}).then(payRes => {
								console.log(payRes)
								//#ifdef MP-WEIXIN
								uni.requestPayment({
									provider: 'wxpay',
									timeStamp: payRes.time_stamp,
									nonceStr: payRes.nonce_str,
									package: payRes.package_,
									signType: payRes.sign_type,
									paySign: payRes.pay_sign,
									success: function(res) {
										// console.log('success:' + JSON.stringify(res));
										that.$u.route({
											url: '/pagesUser/pay_result/pay_result',
										});
									},
									fail: function(err) {
										// console.log('fail:' + JSON.stringify(err));
										that.$u.toast('支付取消')
									}
								});
								//#endif
							}).catch(err => {
								that.$u.toast('支付错误')
							})
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.userinfo {
		display: flex;
		flex-direction: column;
		background-color: white;
		padding: 20rpx;
		margin: 0rpx 20rpx;
		border-radius: 20rpx;

		.user {
			display: flex;
			flex-direction: row;
			align-items: center;

			.info {
				margin-left: 20rpx;
				color: #333333;
				font-weight: 600;
				font-size: 32rpx;
				word-break: break-all;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
				overflow: hidden;
				margin-right: 20rpx;

				.vip {
					display: flex;
					flex-direction: row;
					align-items: center;
					font-size: 24rpx;
					margin-top: 15rpx;

					.viptext {
						margin-left: 10rpx;
						color: #333333;
						font-weight: normal;
					}
				}
			}
		}

		.tip {
			font-size: 28rpx;
			color: #666666;
			margin: 20rpx;
		}
	}

	.card {
		.swiper {
			height: 1500rpx;

			.vip-card {
				display: flex;
				flex-direction: column;
				margin: 10rpx;
				background-color: white;
				border-radius: 20rpx;

				.head {
					display: flex;
					flex-direction: row;
					align-items: center;
					padding: 20rpx;
					background-color: #FF9900;
					border-top-left-radius: 20rpx;
					border-top-right-radius: 20rpx;
					color: white;
					font-size: 32rpx;
				}

				.cont {
					width: 100%;
					padding: 20rpx;

					.item {
						width: 100%;

						.item-head {
							display: flex;
							flex-direction: row;
							align-items: center;
							justify-content: center;
							padding: 10rpx;
							border-top-left-radius: 20rpx;
							border-top-right-radius: 20rpx;
							color: white;
							font-size: 28rpx;
						}

						.item-cont {
							display: flex;
							flex-direction: column;
							align-items: center;
							justify-content: center;
							height: 140rpx;
							border: 1rpx solid #e6e6e6;
							font-size: 35rpx;
							border-bottom-left-radius: 20rpx;
							border-bottom-right-radius: 20rpx;
						}
					}
				}

				.grid-num {
					font-size: 32rpx;
					color: #FF9900;
				}

				.grid-text {
					font-size: 26rpx;
					color: #999999;
				}
			}
		}
	}

	.order {
		margin: 35rpx 20rpx;
		background-color: white;
		border: 1rpx solid #F8F8F8;
		padding: 20rpx;
		border-radius: 10rpx;

		.cell {
			display: flex;
			flex-direction: row;
			align-items: center;
			padding: 20rpx 10rpx;
			font-size: 30rpx;

			.title {
				width: 180rpx;
				color: #666666;
			}

			.cont {
				display: flex;
				flex: 1;
				flex-direction: row;
				justify-content: flex-end;
				color: #F44C01;
				color: 32rpx;
			}
		}

		.order-head {
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #F8F8F8;
		}

		.pay {
			display: flex;
			flex-direction: row;
			align-items: center;
			padding: 20rpx;

			.left {
				flex: 1;
				display: flex;
				flex-direction: row;
				align-items: center;
			}
		}
	}
</style>
